<template>
	<view class="register">
		<view v-if="canIUseGetUserProfile">
			<scroll-view scroll-y="true" style="height:70vh; border-bottom:4rpx solid #efefef;">
				<view class="rich">
					<rich-text :nodes="strings"></rich-text>
				</view>
			</scroll-view>
			<button type="primary" :disabled="status ? true : false" @click="getUserProfile" withCredentials="true">确认同意协议</button>
		</view>
		<view v-else class="version">请先升级微信版本!</view>
	</view>
</template>

<script>
	import paths from "../../config/path.js"
	import {
		post
	} from "../../api/index.js"
	export default {
		data() {
			return {
				strings: "",
				status: false,
				canIUseGetUserProfile: false // 判断组件是否在当前版本可用
			}
		},
		methods: {
			getUserProfile(e) {
				let that = this
				that.status = true
				uni.getUserProfile({
					desc: '确认登录?',
					success: function(data) {
						// console.log(data)
						uni.login({
							success: function(res) {
								let url = paths.login
								post(url, {
									encryptedData: data.encryptedData,
									signature: data.signature,
									rawData: data.rawData,
									iv: data.iv,
									code: res.code
								}).then(res => {
									// console.log(res)
									uni.setStorageSync("userInfo", res.data.data)
									uni.showLoading({
										title: "登录中...",
										success: function() {
											if (res.data.code == "1") {
												if(that.userInfo == "1") {
													uni.reLaunch({
														url: "../index/index?user_info=" + that.userInfo
													})
												} else {
													uni.reLaunch({
														url: "../index/index"
													})
												}
											} else {
												uni.showToast({
													title: res.data.msg,
													icon: "none"
												})
											}
										}
									})
								})
							}
						})
					},
					fail: function(err) {
						uni.showToast({
							title: '登陆失败',
							icon: 'none'
						})
					}
				})
			}
		},
		onLoad(option) {
			console.log(option)
			// 外部扫码进来没有用户id
			if (option.userInfo == "1") {
				this.userInfo = option.userInfo
			}
			// 判断当前版本是否可用
			if (uni.getUserProfile) {
				this.canIUseGetUserProfile = true
			}
		},
		created() {
			let url = paths.agreeMent
			post(url).then(res => {
				this.strings = res.data.data.str
			})
		}
	}
</script>

<style scoped>
	.register {
		width: 100%;
	}

	.rich {
		padding: 20rpx;
		line-height: 38rpx;
	}

	.version {
		text-align: center;
		padding: 20rpx;
		line-height: 38rpx;
	}

	rich-text {
		font-size: 20rpx;
	}

	button {
		width: 702rpx;
		color: #fff;
		margin-top: 134rpx;
	}
</style>
